﻿using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace WebApp.Migrations
{
    public partial class init : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "application",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    UserId = table.Column<int>(nullable: false),
                    AppName = table.Column<string>(maxLength: 50, nullable: false),
                    AppToken = table.Column<string>(maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_application", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "exception_log",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    AppId = table.Column<int>(nullable: false),
                    CollectorCode = table.Column<string>(maxLength: 12, nullable: true),
                    MeterCode = table.Column<string>(maxLength: 12, nullable: true),
                    Command = table.Column<string>(maxLength: 4, nullable: true),
                    RequestId = table.Column<string>(maxLength: 50, nullable: true),
                    CallbackUrl = table.Column<string>(maxLength: 255, nullable: true),
                    Message = table.Column<string>(maxLength: 500, nullable: true),
                    StackTrace = table.Column<string>(maxLength: 1000, nullable: true),
                    CreateTime = table.Column<DateTime>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_exception_log", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "meter_record",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    ActivePower = table.Column<decimal>(nullable: false),
                    TakeTime = table.Column<DateTime>(nullable: false),
                    CollectorCode = table.Column<string>(maxLength: 12, nullable: false),
                    MeterCode = table.Column<string>(maxLength: 12, nullable: false),
                    AppId = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_meter_record", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "user",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Account = table.Column<string>(maxLength: 20, nullable: false),
                    Password = table.Column<string>(maxLength: 50, nullable: false),
                    CreateTime = table.Column<DateTime>(nullable: false),
                    UserName = table.Column<string>(maxLength: 20, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "collector",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    AppId = table.Column<int>(nullable: false),
                    Code = table.Column<string>(maxLength: 12, nullable: false),
                    Address = table.Column<string>(maxLength: 128, nullable: true),
                    HeartbeatTime = table.Column<DateTime>(nullable: false),
                    CreateDate = table.Column<DateTime>(nullable: false),
                    Status = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_collector", x => x.Id);
                    table.ForeignKey(
                        name: "FK_collector_application_AppId",
                        column: x => x.AppId,
                        principalTable: "application",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "operation",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    AppId = table.Column<int>(nullable: false),
                    CollectorCode = table.Column<string>(maxLength: 12, nullable: false),
                    MeterCode = table.Column<string>(maxLength: 12, nullable: false),
                    Password = table.Column<string>(maxLength: 8, nullable: false),
                    RequestId = table.Column<string>(maxLength: 50, nullable: true),
                    Command = table.Column<string>(maxLength: 4, nullable: false),
                    CallbackUrl = table.Column<string>(maxLength: 255, nullable: true),
                    ActivePower = table.Column<decimal>(nullable: false),
                    QueryId = table.Column<string>(maxLength: 50, nullable: false),
                    Status = table.Column<int>(nullable: false),
                    TryCount = table.Column<int>(nullable: false),
                    CreateTime = table.Column<DateTime>(nullable: false),
                    ExcuteTime = table.Column<DateTime>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_operation", x => x.Id);
                    table.ForeignKey(
                        name: "FK_operation_application_AppId",
                        column: x => x.AppId,
                        principalTable: "application",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "subscrebe",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    AppId = table.Column<int>(nullable: false),
                    NotifyType = table.Column<int>(nullable: false),
                    CallbackUrl = table.Column<string>(maxLength: 255, nullable: false),
                    CreateTime = table.Column<DateTime>(nullable: false),
                    Remark = table.Column<string>(maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_subscrebe", x => x.Id);
                    table.ForeignKey(
                        name: "FK_subscrebe_application_AppId",
                        column: x => x.AppId,
                        principalTable: "application",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "meter",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    AppId = table.Column<int>(nullable: false),
                    CollectorId = table.Column<int>(nullable: false),
                    Code = table.Column<string>(maxLength: 12, nullable: false),
                    Address = table.Column<string>(maxLength: 128, nullable: true),
                    Password = table.Column<string>(maxLength: 8, nullable: false),
                    Power = table.Column<decimal>(nullable: false),
                    Create_Date = table.Column<DateTime>(nullable: false),
                    Last_Take_Date = table.Column<DateTime>(nullable: false),
                    Last_Active_Power = table.Column<decimal>(nullable: true),
                    Status = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_meter", x => x.Id);
                    table.ForeignKey(
                        name: "FK_meter_application_AppId",
                        column: x => x.AppId,
                        principalTable: "application",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_meter_collector_CollectorId",
                        column: x => x.CollectorId,
                        principalTable: "collector",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_collector_AppId",
                table: "collector",
                column: "AppId");

            migrationBuilder.CreateIndex(
                name: "IX_meter_AppId",
                table: "meter",
                column: "AppId");

            migrationBuilder.CreateIndex(
                name: "IX_meter_CollectorId",
                table: "meter",
                column: "CollectorId");

            migrationBuilder.CreateIndex(
                name: "IX_operation_AppId",
                table: "operation",
                column: "AppId");

            migrationBuilder.CreateIndex(
                name: "IX_subscrebe_AppId",
                table: "subscrebe",
                column: "AppId");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "exception_log");

            migrationBuilder.DropTable(
                name: "meter");

            migrationBuilder.DropTable(
                name: "meter_record");

            migrationBuilder.DropTable(
                name: "operation");

            migrationBuilder.DropTable(
                name: "subscrebe");

            migrationBuilder.DropTable(
                name: "user");

            migrationBuilder.DropTable(
                name: "collector");

            migrationBuilder.DropTable(
                name: "application");
        }
    }
}
